package com.ruoyi.business.controller;

import java.io.InputStream;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.utils.CreateQRCode;
import com.ruoyi.system.api.model.LoginUser;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.TbBusinessTable;
import com.ruoyi.business.service.ITbBusinessTableService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 桌号Controller
 * 
 * @author ruoyi
 * @date 2023-02-03
 */
@RestController
@RequestMapping("/table")
public class TbBusinessTableController extends BaseController
{
    @Autowired
    private ITbBusinessTableService tbBusinessTableService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询桌号列表
     */
    @RequiresPermissions("business:table:list")
    @GetMapping("/list")
    public TableDataInfo list(TbBusinessTable tbBusinessTable)
    {
        startPage();
        List<TbBusinessTable> list = tbBusinessTableService.selectTbBusinessTableList(tbBusinessTable);
        return getDataTable(list);
    }

    /**
     * 导出桌号列表
     */
    @RequiresPermissions("business:table:export")
    @Log(title = "桌号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBusinessTable tbBusinessTable)
    {
        List<TbBusinessTable> list = tbBusinessTableService.selectTbBusinessTableList(tbBusinessTable);
        ExcelUtil<TbBusinessTable> util = new ExcelUtil<TbBusinessTable>(TbBusinessTable.class);
        util.exportExcel(response, list, "桌号数据");
    }

    /**
     * 获取桌号详细信息
     */
    @RequiresPermissions("business:table:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbBusinessTableService.selectTbBusinessTableById(id));
    }

    /**
     * 新增桌号
     */
    @RequiresPermissions("business:table:add")
    @Log(title = "桌号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbBusinessTable tbBusinessTable)
    {
        return toAjax(tbBusinessTableService.insertTbBusinessTable(tbBusinessTable));
    }

    /**
     * 修改桌号
     */
    @RequiresPermissions("business:table:edit")
    @Log(title = "桌号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbBusinessTable tbBusinessTable)
    {
        return toAjax(tbBusinessTableService.updateTbBusinessTable(tbBusinessTable));
    }

    /**
     * 删除桌号
     */
    @RequiresPermissions("business:table:remove")
    @Log(title = "桌号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbBusinessTableService.deleteTbBusinessTableByIds(ids));
    }

//    @GetMapping("scanningCode")
//    public AjaxResult ScanningCode(@RequestParam("contents") Integer contents,@RequestParam("menuId") Integer menuId){
//        Object qrCode = CreateQRCode.CreateQRCode(String.valueOf(contents));
//        LoginUser loginUser = com.ruoyi.common.security.utils.SecurityUtils.getLoginUser();
//        Integer cuserId = Math.toIntExact(loginUser.getUserid());
//        //保存当前用户id
//        redisTemplate.opsForValue().set(cuserId,menuId);
//        System.out.println(qrCode);
//        return null;
//    }

    @GetMapping("scanningCode")
    public AjaxResult ScanningCode(@RequestBody Map contents){
        Object qrCode = CreateQRCode.CreateQRCode(String.valueOf(contents));
        System.out.println(qrCode);
        return null;
    }
}
